import http from '@/util/axios'
import type { Res, TPage } from '../types'
import * as T from './userManagement_type'

export enum ApiPath {
  List = '/edata-system/api/system/user/list', // 用户列表
  CreateUser = '/edata-system/api/system/user/create', // 创建用户
  UpdateUser = '/edata-system/api/system/user/update', // 创建用户
  GetUserInfo = '/edata-system/api/system/user/detail', // 获取用户信息
  DeleteUser = '/edata-system/api/system/user/delete', // 删除用户
  
  RoleMenus = '/edata-system/api/system/role/menu/query', // 指定角色菜单
  SysMenuTree = '/edata-system/api/system/menu/tree', // 菜单树
  OrgTreeList = '/edata-system/api/system/org/tree', // 组织树
  RoleOrg = '/edata-system/api/system/role/org/query', // 角色组织查询
  RoleMenu = '/edata-system/api/system/role/menu/query', // 角色菜单查询

  RoleList = '/edata-system/api/system/role/list', // 角色列表
  UserBindRoles = '/edata-system/api/system/user/role/create', // 用户绑定角色
  UserUnBindRoles = '/edata-system/api/system/user/role/delete', // 用户解绑角色
  UserRoles = '/edata-system/api/system/user/role/list', // 用户角色列表

  RoleAddMenu = '/edata-system/api/system/role/menu/upsert', // 角色添加菜单
  RoleAddOrg = '/edata-system/api/system/role/org/upsert', // 角色添加组织

  UserTempAuths = '/edata-system/api/system/user/temp_permission/create', // 用户临时权限
}

// 角色添加组织
export function roleAddOrg(role_id: number, org_code: number[]): Promise<Res<any>> {
  return http.post(ApiPath.RoleAddOrg, { role_id, org_code })
}

// 角色添加菜单
export function roleAddMenu(role_id: number, menu_code: number[]): Promise<Res<any>> {
  return http.post(ApiPath.RoleAddMenu, { role_id, menu_code })
}

// 角色组织
export function getRoleOrg(role_id: number): Promise<Res<{
  org_code: number,
  org_name: string,
  org_type: string
}[]>> {
  return http.get(ApiPath.RoleOrg, { params: { role_id } })
}

// 角色菜单
export function getRoleMenu(role_id: number): Promise<Res<{
  menu_code: number,
  menu_name: string,
  menu_type: string
}[]>> {
  return http.get(ApiPath.RoleMenu, { params: { role_id } })
}



// 获得列表
export function getList(page: TPage, filter?: any, ): Promise<Res<T.TUserItem[]>> {
  const params: {[key:string]: any} = {
    ...{
      page: 1,
      page_size: 10,
    },
    ...page, ...filter
  }
  const paramArr: string[] = []
  for(const key in params) {
    paramArr.push(`${key}=${params[key]}`)
  }
  return http.get(`${ApiPath.List}?${paramArr.join('&')}`, {})
}

// 创建用户
export function createUser(data: T.TUserItem): Promise<Res<any>> {
  return http.post(ApiPath.CreateUser, data)
}

// 更新用户
export function updateUser(data: T.TUserItem): Promise<Res<any>> {
  return http.post(ApiPath.UpdateUser, data)
}

// 获取用户信息
export function getUserInfo(account: string): Promise<Res<T.TUserItem>> {
  return http.get(`${ApiPath.GetUserInfo}?account=${account}`, {})
}

// 删除用户
export function deleteUser(account: string): Promise<Res<any>> {
  return http.get(`${ApiPath.DeleteUser}?account=${account}`, {})
}


// 获得角色列表
export function getRoleList(page: TPage, role_name: string = ''): Promise<Res<T.TRoleItem[]>> {
  return http.get(`${ApiPath.RoleList}?page=${page.page}&page_size=${page.page_size}&role_name=${role_name}`, {})
}

// 获得角色菜单
export function getRoleMenus(role_id: string | number): Promise<Res<T.TRoleMenuItem[]>> {
  return http.get(`${ApiPath.RoleMenus}?role_id=${role_id}`, {})
}

// 获得系统菜单树
export function getSysMenuTree(service_name: string): Promise<Res<any>> {
  return http.get(ApiPath.SysMenuTree, { params: {service_name} })
}

// 获得用户角色列表
export function getUserRoles(account: string, page: TPage, role_name = ''): Promise<Res<T.TUserRoleItem[]>> {
  return http.get(`${ApiPath.UserRoles}?page=${page.page}&page_size=${page.page_size}&account=${account}&role_name=${role_name}`, {})
}

// 用户绑定角色
export function userBindRoles(account: string, role_id: string[]): Promise<Res<any>> {
  return http.post(ApiPath.UserBindRoles, { account, role_id })
}
// 用户解绑角色
export function userUnBindRoles(account: string, role_id: number): Promise<Res<any>> {
  return http.get(ApiPath.UserUnBindRoles, { params: {account, role_id} })
}

// 获得组织树
export function getOrgTreeList(): Promise<Res<T.TOrgItem>> {
  return http.get(ApiPath.OrgTreeList, {})
}

// 用户临时权限
export function userTempAuths(account: string, menu_code: number[], org_code: number[] , role_name: string): Promise<Res<any>> {
  return http.post(ApiPath.UserTempAuths, { account, menu_code, org_code, role_name })
}